/**
 * Title: WADS
 * Description: Web-based Auto flow Documents System
 * Copyright: 2007
 * Company CoE
 * @author Old Developer team
 * @version 1.1
 */

package wads.app;

import wads.drawing.line.CaseLink;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

abstract public class CaseLinkDialog extends JDialog {
  private String LinkLabel = "";
  
//  private CaseLink link;

  protected JPanel ControlsPanel;
  private JPanel ButtonsPanel;

  public CaseLinkDialog(Frame parent, CaseLink owner) {
    super(parent);
    
//    link = owner;
    
    //Init Dialog Component.
    try {
      jbInit();
    }
    catch(Exception e) {
      System.out.println("Exception thrown from CaseLinkDialog while calling jbInit() to initialize Dialog components.");
      e.printStackTrace();
    }

    /*
    //Init patterns in combo box
    String[] stereoTypeOptions = { "", "<<Actor>>", "<<Communicates>>",
        "<<Extends>>", "<<Includes>>", "<<Interface>>", "<<Uses>>" };
        setStereoType(stereoTypeOptions[0]);

    // Set up the UI for selecting a stereotype.
    JLabel stereoTypeLabel = new JLabel("Stereotype");
    stereoTypeLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

    JComboBox stereoTypeCombo = new JComboBox(stereoTypeOptions);
    stereoTypeCombo.setEditable(false);
    stereoTypeCombo.setSelectedIndex(0);
    stereoTypeCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
    StereoTypeComboListener stereoTypeListener = new StereoTypeComboListener();
    stereoTypeCombo.addActionListener(stereoTypeListener);

    // Set up the UI for selecting cardinalities.
    JLabel cardinalityStartLabel = new JLabel("Cardinality at start node");
    JLabel cardinalityEndLabel = new JLabel("Cardinality at end node");
    cardinalityStartLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    cardinalityEndLabel.setAlignmentX(Component.LEFT_ALIGNMENT);

    String[] cardinalityOptions = { "", "0", "1", "0..*", "1..*", "*" };
    JComboBox cardinalityAtStartNodeCombo = new JComboBox(cardinalityOptions);
    cardinalityAtStartNodeCombo.setEditable(true);
    cardinalityAtStartNodeCombo.setSelectedIndex(0);
    cardinalityAtStartNodeCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
    CardinalityComboListener cardinalityAtStartNodeListener = new CardinalityComboListener(CardinalityComboListener.START_NODE);
    cardinalityAtStartNodeCombo.addActionListener(cardinalityAtStartNodeListener);

    JComboBox cardinalityAtEndNodeCombo = new JComboBox(cardinalityOptions);
    cardinalityAtEndNodeCombo.setEditable(true);
    cardinalityAtEndNodeCombo.setSelectedIndex(0);
    cardinalityAtEndNodeCombo.setAlignmentX(Component.LEFT_ALIGNMENT);
    CardinalityComboListener cardinalityAtEndNodeListener = new CardinalityComboListener(CardinalityComboListener.END_NODE);
    cardinalityAtEndNodeCombo.addActionListener(cardinalityAtEndNodeListener);

    // Set up the UI for entering input
    JLabel labelInputLabel = new JLabel("Label");
    labelInputLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
    labelField = new JTextField("New link");
    labelField.setAlignmentX(Component.LEFT_ALIGNMENT);

    // Lay out everything
    JPanel stereoTypePanel = new JPanel();
    stereoTypePanel.setLayout(new GridLayout(2, 1));
    stereoTypePanel.add(stereoTypeLabel);
    stereoTypePanel.add(stereoTypeCombo);

    JPanel cardinalityStartPanel = new JPanel();
    cardinalityStartPanel.setLayout(new GridLayout(2, 1));
    cardinalityStartPanel.add(cardinalityStartLabel);
    cardinalityStartPanel.add(cardinalityAtStartNodeCombo);

    JPanel cardinalityEndPanel = new JPanel();
    cardinalityEndPanel.setLayout(new GridLayout(2, 1));
    cardinalityEndPanel.add(cardinalityEndLabel);
    cardinalityEndPanel.add(cardinalityAtEndNodeCombo);

    JPanel labelInputPanel = new JPanel();
    labelInputPanel.setLayout(new GridLayout(2, 1));
    labelInputPanel.add(labelInputLabel);
    labelInputPanel.add(labelField);
    */
  }
  
  private void jbInit() throws Exception {
    Container c = this.getContentPane();
    c.setLayout(new BorderLayout());
    ControlsPanel = new JPanel();
    ControlsPanel.setLayout(new BoxLayout(ControlsPanel, BoxLayout.Y_AXIS));
    ControlsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
    ButtonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
    
    //Concrete buttons panel.
    JButton OKButton = new JButton("OK");
    OKButton.addActionListener(new ActionListener() {
            @Override
      public void actionPerformed(ActionEvent ae) {
        OKButton_actionPerformed(ae);
      }
    });
    JButton CancelButton = new JButton("Cancel");
    CancelButton.addActionListener(new ActionListener() {
            @Override
      public void actionPerformed(ActionEvent ae) {
        CancelButton_actionPerformed(ae);
      }
    });
    
    ButtonsPanel.add(OKButton);
    ButtonsPanel.add(Box.createRigidArea(new Dimension(10, 0)));
    ButtonsPanel.add(CancelButton);
    
    c.add(ControlsPanel, "Center");
    c.add(ButtonsPanel, "South");
    
    this.setModal(true);
    this.setResizable(false);
  }

  /*
  public void setStereoType(String stereoType){
    this.stereoType = stereoType;
  }
  public String getStereoType() {
    return stereoType;
  }

  public void setLabel(String label) {
    this.LinkLabel = label;
  }
  public String getLabel() {
    return LinkLabel;
  }

  public void setCardinalityAtEndNode(String cardinalityAtEndNode) {
    this.cardinalityAtEndNode = cardinalityAtEndNode;
  }

  public void setCardinalityAtStartNode(String cardinalityAtStartNode) {
    this.cardinalityAtStartNode = cardinalityAtStartNode;
  }
  

  /**
   * @return String as cardinality of END edge of link.
   *
  public String getCardinalityAtEndNode() {
    return cardinalityAtEndNode;
  }

  /**
   * @return String as cardinality of START edge of link.
   *
  public String getCardinalityAtStartNode() {
    return cardinalityAtStartNode;
  }*/

  /*
  // Listens to the cardinality combo box.
  private class StereoTypeComboListener implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      setStereoType(((JComboBox)e.getSource()).getSelectedItem().toString());
    }
  }

  // Listens to the cardinality combo box.
  private class CardinalityComboListener implements ActionListener {
    public final static int START_NODE = 0;
    public final static int END_NODE = 1;

    int whichEnd;

    public CardinalityComboListener(int whichEnd) {
      this.whichEnd = whichEnd;
    }

    public void actionPerformed(ActionEvent e) {
      if(whichEnd == START_NODE)
        setCardinalityAtStartNode(((JComboBox)e.getSource()).getSelectedItem().toString());
      else
        setCardinalityAtEndNode(((JComboBox)e.getSource()).getSelectedItem().toString());
    }
  }*/

//  public CaseLink getLink() {
//    return link;
//  }
//
//  public void setLink(CaseLink ownerLink) {
//    link = ownerLink;
//  }
  
  abstract public void OKButton_actionPerformed(ActionEvent ae);
  abstract public void CancelButton_actionPerformed(ActionEvent ae);

  /**
   * @return Link Label.
   */
  public String getLinkLabel() {
    return LinkLabel;
  }

  /**
   * @param linkLabel the linkLabel to set
   */
  public void setLinkLabel(String linkLabel) {
    LinkLabel = linkLabel;
  }
  
  public void addControl(JComponent jc) {
    ControlsPanel.add(jc);
    ControlsPanel.add(Box.createRigidArea(new Dimension(0, 20)));
  }
}
